package c.a.a.b0.l;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable.Callback;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import c.a.a.a0.b;
import c.a.a.e0.g;
import c.a.a.f0.c;
import c.a.a.l;
import c.a.a.m;
import c.a.a.q;
import c.a.a.z.c.a;
import c.a.a.z.c.p;
import java.io.IOException;

/* compiled from: ImageLayer */
public class d extends b {
    public a<ColorFilter, ColorFilter> A;
    public final Paint x = new c.a.a.z.a(3);
    public final Rect y = new Rect();
    public final Rect z = new Rect();

    public d(l lVar, e eVar) {
        super(lVar, eVar);
    }

    public void b(RectF rectF, Matrix matrix, boolean z) {
        super.b(rectF, matrix, z);
        Bitmap s = s();
        if (s != null) {
            rectF.set(0.0f, 0.0f, g.c() * ((float) s.getWidth()), g.c() * ((float) s.getHeight()));
            this.m.mapRect(rectF);
        }
    }

    public <T> void i(T t, c<T> cVar) {
        this.v.c(t, cVar);
        if (t != q.C) {
            return;
        }
        if (cVar == null) {
            this.A = null;
        } else {
            this.A = new p(cVar, null);
        }
    }

    public void l(Canvas canvas, Matrix matrix, int i) {
        Bitmap s = s();
        if (s != null && !s.isRecycled()) {
            float c = g.c();
            this.x.setAlpha(i);
            a aVar = this.A;
            if (aVar != null) {
                this.x.setColorFilter((ColorFilter) aVar.e());
            }
            canvas.save();
            canvas.concat(matrix);
            this.y.set(0, 0, s.getWidth(), s.getHeight());
            this.z.set(0, 0, (int) (((float) s.getWidth()) * c), (int) (((float) s.getHeight()) * c));
            canvas.drawBitmap(s, this.y, this.z, this.x);
            canvas.restore();
        }
    }

    public final Bitmap s() {
        b bVar;
        String str = this.o.g;
        l lVar = this.n;
        if (lVar.getCallback() == null) {
            bVar = null;
        } else {
            b bVar2 = lVar.j;
            if (bVar2 != null) {
                Callback callback = lVar.getCallback();
                Object context = (callback != null && (callback instanceof View)) ? ((View) callback).getContext() : null;
                Object obj = ((context == null && bVar2.a == null) || bVar2.a.equals(context)) ? 1 : null;
                if (obj == null) {
                    lVar.j = null;
                }
            }
            if (lVar.j == null) {
                lVar.j = new b(lVar.getCallback(), lVar.k, lVar.l, lVar.b.d);
            }
            bVar = lVar.j;
        }
        if (bVar == null) {
            return null;
        }
        m mVar = (m) bVar.d.get(str);
        if (mVar == null) {
            return null;
        }
        Bitmap bitmap = mVar.e;
        if (bitmap != null) {
            return bitmap;
        }
        c.a.a.b bVar3 = bVar.c;
        Bitmap a;
        if (bVar3 != null) {
            a = bVar3.a(mVar);
            if (a == null) {
                return a;
            }
            bVar.a(str, a);
            return a;
        }
        String str2 = mVar.d;
        Options options = new Options();
        options.inScaled = true;
        options.inDensity = 160;
        if (!str2.startsWith("data:") || str2.indexOf("base64,") <= 0) {
            try {
                if (TextUtils.isEmpty(bVar.b)) {
                    throw new IllegalStateException("You must set an images folder before loading an image. Set it with LottieComposition#setImagesFolder or LottieDrawable#setImagesFolder");
                }
                AssetManager assets = bVar.a.getAssets();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(bVar.b);
                stringBuilder.append(str2);
                a = g.e(BitmapFactory.decodeStream(assets.open(stringBuilder.toString()), null, options), mVar.a, mVar.b);
                bVar.a(str, a);
                return a;
            } catch (IOException e) {
                c.a.a.e0.c.c("Unable to open asset.", e);
                return null;
            }
        }
        try {
            byte[] decode = Base64.decode(str2.substring(str2.indexOf(44) + 1), 0);
            a = BitmapFactory.decodeByteArray(decode, 0, decode.length, options);
            bVar.a(str, a);
            return a;
        } catch (IllegalArgumentException e2) {
            c.a.a.e0.c.c("data URL did not have correct base64 format.", e2);
            return null;
        }
    }
}
